"""
@Project ：HaloInterfaceProject 
@File    ：ArticleModuleTest.py
@IDE     ：PyCharm 
@Author  ：张成龙
@Date    ：2024/10/23 10:46 
@explain ：
"""

import logging

import allure
import pytest

from api.ArticleModule.CreateArticleCategories import CreateCategoriesType
from api.ArticleModule.DeleteArticleCategories import DeleteArticleCategories
from api.ArticleModule.GetArticleCategories import GetArticleCategories
from utils.AllureAttachReqRespUtil import AllureAttachReqRespUtil
from utils.WebCookie import WebCookie


@pytest.mark.run(order=1)
@allure.epic("后台管理")
@allure.feature("文章管理")
@allure.description("文章管理模块")
@pytest.mark.article
class TestArticleModuleTest:

    category_name = None

    def setup_class(self):
        # 通过 selenium 无头模式 动态获取 cookie
        WebCookie.get_cookies()
        self.create_categories_type = CreateCategoriesType()
        self.get_article_categories = GetArticleCategories()
        self.delete_article_categories = DeleteArticleCategories()

    @allure.step(title="新增文章所有分类")
    @allure.severity(allure.severity_level.BLOCKER)
    def test_create_article_categories(self):
        response = self.create_categories_type.create_categories_type()

        AllureAttachReqRespUtil.attach_request_response(response.request, response)

        logging.info("create_article_categories response = {}".format(response.text))

        assert response.status_code == 201

        assert response.json()["kind"] == "Category"

    @allure.step(title="获取文章所有分类")
    @allure.severity(allure.severity_level.CRITICAL)
    def test_get_article_categories(self):

        response = self.get_article_categories.get_article_categories()

        TestArticleModuleTest.category_name = response.json()["items"][0]["metadata"]["name"]

        print("======================" + self.category_name + "===============")

        AllureAttachReqRespUtil.attach_request_response(response.request, response)
        logging.info("get_article_categories response = {}".format(response.text))

        assert response.status_code == 200
        assert response.json()["items"][0]["kind"] == "Category"

    @allure.step(title="删除文章所有分类")
    @allure.severity(allure.severity_level.BLOCKER)
    def test_delete_article_categories(self):

        response = self.delete_article_categories.delete_article_categories(TestArticleModuleTest.category_name)
        AllureAttachReqRespUtil.attach_request_response(response.request, response)
        logging.info("get_article_categories response = {}".format(response.text))
        assert response.status_code == 200
        assert response.json()["kind"] == "Category"





